<?php
function createCheckCode($width=65,$height=26,$num_code=5,$num_disturb_points=200){ 
        $img=imagecreate($width,$height);
	    $bg_color=imagecolorallocate($img,255,127,0);
	    $border_color=imagecolorallocate($img,255,255,255);
	    imagerectangle($img,0,0,$width-1,$height-1,$border_color);
	    $rand_num=rand();
		$str=md5($rand_num);
		$str_code=strtoupper(substr($str,0,$num_code));
		for($i=0;$i<$num_code;++$i){ 
	         $str_color=imagecolorallocate($img,255,255,255);
	         $font_size=5;
	         $str_x=floor(($width/$num_code)*$i)+rand(0,5);
	         $str_y=rand(2,$height-15);
	         imagechar($img,$font_size,$str_x,$str_y,$str_code[$i],$str_color);
	     }
         header("Content-type:image/png");
         imagepng($img);
         imagedestroy($img);
         return $str_code;
     }
if($_GET["action"]=="cityckcode")
{
    $_SESSION['str_code'] =createCheckCode();
	exit;
}
?>

checkcode.php
<html>
<head>
<script language="JavaScript">
<!--
function change(id){
document.getElementById(id).src ='do.php?action=ckcode&t='+Math.random(1);
}
//-->
</script>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
</head>
   <body>
          <div class="floor_hao_hang">验证码 ：<input type="text" name="checkcode" id="checkcode" class="text5" value="" /><img id="imgcheckcode" src="dodo.php?action=cityckcode" /><a href="javascript:change('imgcheckcode')">换一个</a></div>
   </body>
</html>